//-------------- BEGIN CallBackObject --------------------
function CallBackObject()
{
  this.XmlHttp = this.GetHttpObject();
}
 
CallBackObject.prototype.GetHttpObject = function()
{ 
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
 
CallBackObject.prototype.DoCallBack = function(eventTarget, eventArgument)
{
  var theData = '';
  var theform = document.forms[0];
  var thePage = window.location.pathname + window.location.search;
  var eName = '';
 
  theData  = '__EVENTTARGET='  + escape(eventTarget.split("$").join(":")) + '&';
  theData += '__EVENTARGUMENT=' + eventArgument + '&';
  theData += '__VIEWSTATE='    + escape(theform.__VIEWSTATE.value).replace(new RegExp('\\+', 'g'), '%2b') + '&';
  theData += 'IsCallBack=true&';
  for( var i=0; i<theform.elements.length; i++ )
  {
    eName = theform.elements[i].name;
    if( eName && eName != '')
    {
      if( eName == '__EVENTTARGET' || eName == '__EVENTARGUMENT' || eName == '__VIEWSTATE' )
      {
        // Do Nothing
      }
      else
      {
        theData = theData + escape(eName.split("$").join(":")) + '=' + theform.elements[i].value;
        if( i != theform.elements.length - 1 )
          theData = theData + '&';
      }
    }
  }
 
  if( this.XmlHttp )
  {
    if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
    {
      var oThis = this;
      this.XmlHttp.open('POST', thePage, true);
      this.XmlHttp.onreadystatechange = function(){ oThis.ReadyStateChange(); };
      this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      this.XmlHttp.send(theData);
    }
  }
}
 
CallBackObject.prototype.AbortCallBack = function()
{
  if( this.XmlHttp )
    this.XmlHttp.abort();
}
 
CallBackObject.prototype.OnLoading = function()
{
  // Loading
}
 
CallBackObject.prototype.OnLoaded = function()
{
  // Loaded
}
 
CallBackObject.prototype.OnInteractive = function()
{
  // Interactive
}
 
CallBackObject.prototype.OnComplete = function(responseText, responseXml)
{
  // Complete
}
 
CallBackObject.prototype.OnAbort = function()
{
  // Abort
}
 
CallBackObject.prototype.OnError = function(status, statusText)
{
  // Error
}
 
CallBackObject.prototype.ReadyStateChange = function()
{
  if( this.XmlHttp.readyState == 1 )
  {
    this.OnLoading();
  }
  else if( this.XmlHttp.readyState == 2 )
  {
    this.OnLoaded();
  }
  else if( this.XmlHttp.readyState == 3 )
  {
    this.OnInteractive();
  }
  else if( this.XmlHttp.readyState == 4 )
  {
    if( this.XmlHttp.status == 0 )
      this.OnAbort();
    else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" )
      this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
    else
      this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);   
  }
}
//----------------End CallBackObject---------------------

//----------------BEGIN AutoCompleteTextBox ------------------------

function AutoCompleteTextBox(TextBoxId, DivId, DivClass)
{
	// initialize member variables
	var oThis = this;
	var oText = document.getElementById(TextBoxId);
	var oDiv  = document.getElementById(DivId);
	this.TextBox = oText;
	this.Div = oDiv;
	
	// CallBackObject + Event Handlers
	this.Cbo = new CallBackObject();
	this.Cbo.OnComplete = function(responseText,responseXML){oThis.Cbo_Complete(responseText,responseXML);};
	this.Cbo.OnError    = function(status,statusText,responseText){oThis.Cbo_Error(status,statusText,responseText);};
			
	// attach handlers to the TextBox
	oText.AutoCompleteTextBox = this;
	oText.onkeyup = AutoCompleteTextBox.prototype.OnKeyUp;
	oText.onblur  = AutoCompleteTextBox.prototype.OnBlur;
	
	// align the drop down div
	var c = GetCoords(oText);
	var n = oText.style.pixelHeight;
	if( !n )
	{ 
	  n = 25;
	}
	else
	{
	  n += 2;
	}
	oDiv.style.left = c.x;
	oDiv.style.top = c.y + n;
	oDiv.style.display = 'none';
	oDiv.style.position = 'absolute';
	
	// Set some default styles
	if( DivClass )
	  oDiv.className = DivClass;
	else
	{
	  oDiv.style.border = '1';
	  oDiv.style.borderColor = 'black';
	  oDiv.style.borderStyle = 'solid';
	  oDiv.style.backgroundColor = 'white';
	  oDiv.style.padding = '2';
	}
}

AutoCompleteTextBox.prototype.DoAutoSuggest = false;

AutoCompleteTextBox.prototype.ListItemClass = '';

AutoCompleteTextBox.prototype.ListItemHoverClass = '';

// TextBox OnBlur
AutoCompleteTextBox.prototype.OnBlur = function()
{
	this.AutoCompleteTextBox.TextBox_Blur();
}

AutoCompleteTextBox.prototype.TextBox_Blur = function()
{
	this.Div.style.display='none';
}

// TextBox OnKeyUp
AutoCompleteTextBox.prototype.OnKeyUp = function(oEvent)
{
  //check for the proper location of the event object
  if (!oEvent) 
  {
    oEvent = window.event;
  }    
  this.AutoCompleteTextBox.TextBox_KeyUp(oEvent);      
}

AutoCompleteTextBox.prototype.TextBox_KeyUp = function(oEvent)
{
  var iKeyCode = oEvent.keyCode;

  if( iKeyCode == 8 )
  {
    this.Div.innerHTML = '';
		this.Div.style.display = 'none';
		return;
  }
  else if( iKeyCode == 16 || iKeyCode == 20 )
  {
    this.DoAutoSuggest = true;
  }
  else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) 
  {
    return;
  }
  else
  {
    this.DoAutoSuggest = true;
  }
  
	var txt = this.TextBox.value;
	if( txt.length > 0 )
	{
	  this.Cbo.DoCallBack(this.TextBox.name, txt);
	}
	else
	{
	  this.Div.innerHTML = '';
		this.Div.style.display = 'none';
		this.Cbo.AbortCallBack();
	}
}

AutoCompleteTextBox.prototype.Cbo_Complete = function(responseText, responseXML)
{ 
  while ( this.Div.hasChildNodes() )
	  this.Div.removeChild(this.Div.firstChild);
			
	// get all the matching strings from the server response
	var aStr = responseText.split('\n');
			
	// add each string to the popup-div
	var i, n = aStr.length;
	
	if( n > 0 )
	{
	  for ( i = 0; i < n; i++ )
	  {
		  var oDiv = document.createElement('div');
		  this.Div.appendChild(oDiv);
		  try
		  {
		    oDiv.innerHTML    = aStr[i];
		  }
		  catch(e)
		  {
		    this.Cbo_Error('405','Error','Text returned from Call Back was invalid');
		    return;
		  }
		  oDiv.noWrap       = true;
		  oDiv.style.width  = '100%';
		  oDiv.className    = this.ListItemClass;
		  oDiv.onmousedown  = AutoCompleteTextBox.prototype.Div_MouseDown;
		  oDiv.onmouseover  = AutoCompleteTextBox.prototype.Div_MouseOver;
		  oDiv.onmouseout   = AutoCompleteTextBox.prototype.Div_MouseOut;
		  oDiv.AutoCompleteTextBox = this;			
	  }
	  this.Div.style.display = 'block';
	  
	  if( this.DoAutoSuggest == true )
	    this.AutoSuggest( aStr );
	}
	else
	{
	  this.Div.innerHTML = '';
		this.Div.style.display='none';
	}
}

AutoCompleteTextBox.prototype.Cbo_Error = function(status, statusText, responseText)
{
  alert('CallBackObject Error: status=' + status + '\nstatusText=' + statusText + '\n' + responseText);
}

AutoCompleteTextBox.prototype.Div_MouseDown = function()
{
	this.AutoCompleteTextBox.TextBox.value = this.innerHTML;
}

AutoCompleteTextBox.prototype.Div_MouseOver = function()
{
  if( this.AutoCompleteTextBox.ListItemHoverClass.length > 0 )
	  this.className = this.AutoCompleteTextBox.ListItemHoverClass;
	else
	{
	  this.style.backgroundColor = 'black';
    this.style.color = 'white';
	}
}

AutoCompleteTextBox.prototype.Div_MouseOut = function()
{
  if( this.AutoCompleteTextBox.ListItemClass.length > 0 )
	  this.className = this.AutoCompleteTextBox.ListItemClass;
	else
	{
	  this.style.backgroundColor = 'white';
    this.style.color = 'black';
	}
}

AutoCompleteTextBox.prototype.AutoSuggest = function(aSuggestions /*:array*/) 
{
  if (aSuggestions.length > 0) 
  {
    this.TypeAhead(aSuggestions[0]);
  }
}

AutoCompleteTextBox.prototype.TypeAhead = function( sSuggestion /*:string*/)
{
  if( this.TextBox.createTextRange || this.TextBox.setSelectionRange)
  {
    var iLen = this.TextBox.value.length; 
    this.TextBox.value = sSuggestion; 
    this.SelectRange(iLen, sSuggestion.length);
  }
}

AutoCompleteTextBox.prototype.SelectRange = function (iStart /*:int*/, iLength /*:int*/) 
{
  //use text ranges for Internet Explorer
  if (this.TextBox.createTextRange) 
  {
    var oRange = this.TextBox.createTextRange(); 
    oRange.moveStart("character", iStart); 
    oRange.moveEnd("character", iLength - this.TextBox.value.length);      
    oRange.select();
   
  //use setSelectionRange() for Mozilla
  } 
  else if (this.TextBox.setSelectionRange) 
  {
      this.TextBox.setSelectionRange(iStart, iLength);
  }     

  //set focus back to the textbox
  this.TextBox.focus();      
}

//----------------END AutoCompleteTextBox ------------------------
             
function GetCoords(obj /*:object*/) 
{   
  var newObj = new Object();
  newObj.x = obj.offsetLeft;
  newObj.y = obj.offsetTop;
  theParent = obj.offsetParent;
  while(theParent != null)
  {
    newObj.y += theParent.offsetTop;
    newObj.x += theParent.offsetLeft;
    theParent = theParent.offsetParent;
  }
  
  return newObj;
}